package com.suyang.syt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.model.Role;
import com.suyang.syt.model.User;
import com.suyang.syt.model.UserRole;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.OperationPermission;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.RoleDTO;
import com.suyang.syt.model.dto.UserRoleDTO;
import com.suyang.syt.service.interfaces.RoleService;
import com.suyang.syt.service.interfaces.UserRoleService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/role")
@AuthClass
public class AdminRoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserRoleService userRoleService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		System.out.println("------------------------------");
		return "role/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<Role> list(HttpServletRequest request, int page, int rows, String sort, String order, String name, String roleType) {
		try {
			int type = 0;
			if (EmptyUtil.isNotEmpty(roleType)) {
				type = Integer.parseInt(roleType);
			}
			return roleService.listByEasyUI(page, rows, sort, order, name, type);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/fun", method = RequestMethod.POST)
	public String functiom(HttpServletRequest request, String id, Model model) {
		model.addAttribute("rid", id);
		return "role/roleSet";
	}

	@RequestMapping(value = "/findAll", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<Role> findAll(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			return roleService.listByEasyUI(page, rows, sort, order, null, 0);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/operatiaonList", method = RequestMethod.POST)
	public String operatiaonList(HttpServletRequest request, long roleId, long resourceId) {
		try {
			List<OperationPermission> operationPermissions = roleService.operatiaonList(roleId, resourceId);
			request.setAttribute("resourceId", resourceId);

			request.setAttribute("operationList", operationPermissions);
			return "role/operationListForFunction";
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/updateResource", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updateResource(HttpServletRequest request, String resourceIds, long roleId) {
		try {
			return roleService.updateResource(roleId, resourceIds);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/updatePermission", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updatePermission(HttpServletRequest request, long resourceId, long roleId, String permissionIds) {
		try {
			return roleService.updatePermission(roleId, resourceId, permissionIds);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, RoleDTO roleDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user != null) {
				roleDTO.setCreateName(user.getUsername());
				roleDTO.setCreateTime(DateFormat.dateToString(new Date()));
			}
			return roleService.add(roleDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			return roleService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, RoleDTO roleDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user != null) {
				roleDTO.setModifyName(user.getUsername());
				roleDTO.setModifyTime(DateFormat.dateToString(new Date()));
			}
			return roleService.update(roleDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}

	}

	@RequestMapping(value = "/clearUser", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updateclearUser(HttpServletRequest request, String roleIds) {
		try {
			return roleService.deleteClearUser(roleIds);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/user/list", method = RequestMethod.GET)
	public String listGroupUser(String rid, Model model) {
		if (EmptyUtil.isNotEmpty("rid")) {
			model.addAttribute("rid", rid);
		}
		return "role/user/list";
	}

	@RequestMapping(value = "/user/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserRoleDTO> listGroupUser(HttpServletRequest request, String rid, int page, int rows, String sort, String order) {
		try {
			long roleId = Long.parseLong(rid);
			return userRoleService.listByEasyUI(2, roleId, page, rows, sort, order);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/user/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addUserGroup(HttpServletRequest request, UserRole userRole) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user != null) {
				userRole.setCreateName(user.getUsername());
				userRole.setCreateTime(DateFormat.dateToString(new Date()));
			}
			return userRoleService.add(userRole);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/user/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteUserGroup(HttpServletRequest request, String ids) {
		try {
			return userRoleService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}